<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "user".
 *
 * @property integer $id
 * @property string $user_name
 * @property integer $sex
 * @property string $signature
 * @property string $phone
 * @property string $email
 * @property string $avatar
 * @property string $stu_num
 * @property string $password
 * @property integer $is_admin
 * @property string $vector_str
 * @property string $ctime
 *
 * @property Collect[] $collects
 * @property Audio[] $collectAudios
 * @property Comment[] $comments
 * @property Praise[] $praises
 * @property Audio[] $praiseAudios
 * @property array $vector
 */
class User extends \yii\db\ActiveRecord {
    public static  $sexArray = ['未知', '男', '女',];
    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'user';
    }

    // 检查密码是否正确
    public function validatePassword($password) {
        return sha1($password) == $this->password;
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['user_name', 'password'], 'required'],
            [['vector_str'], 'string'],
            [['sex', 'is_admin'], 'integer'],
            [['ctime'], 'safe'],
            [['user_name'], 'string', 'max' => 100],
            [['signature', 'phone', 'email', 'avatar', 'stu_num', 'password'], 'string', 'max' => 255],
            [['user_name'], 'unique'],
            [['phone'], 'unique'],
            [['email'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => '用户id',
            'user_name' => '用户名',
            'sex' => '性别',
            'signature' => '签名',
            'phone' => '电话',
            'email' => '电子邮件',
            'avatar' => '用户头像',
            'stu_num' => '学号',
            'password' => '密码',
            'is_admin' => '是否为管理员',
            'vector_str' => 'Vector',
            'ctime' => 'Ctime',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCollects() {
        return $this->hasMany(Collect::className(), ['user_id' => 'id'])->inverseOf('user');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCollectAudios() {
        return $this->hasMany(Audio::className(), ['id' => 'audio_id'])->viaTable('collect', ['user_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getComments() {
        return $this->hasMany(Comment::className(), ['user_id' => 'id'])->inverseOf('user');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPraises() {
        return $this->hasMany(Praise::className(), ['user_id' => 'id'])->inverseOf('user');
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPraiseAudios() {
        return $this->hasMany(Audio::className(), ['id' => 'audio_id'])->viaTable('praise', ['user_id' => 'id']);
    }


    public function getVector() {
        return unserialize($this->vector_str);
    }
}
